package com.demo.las.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDate;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "personal_affairs", indexes = {
    @Index(name = "idx_personal_affairs_user_id", columnList = "user_id"),
    @Index(name = "idx_personal_affairs_code", columnList = "affair_code", unique = true)
})
public class PersonalAffair {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "用户ID不能为空")
    @Column(name = "user_id", nullable = false)
    private Long userId;

    @NotBlank(message = "事务名称不能为空")
    @Size(max = 200, message = "事务名称长度不能超过200个字符")
    @Column(name = "affair_name", nullable = false)
    private String affairName;

    @NotBlank(message = "事务编号不能为空")
    @Size(max = 100, message = "事务编号长度不能超过100个字符")
    @Column(name = "affair_code", nullable = false, unique = true)
    private String affairCode;

    @NotNull(message = "开始时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "start_date", nullable = false)
    private LocalDateTime startDate;

    @NotNull(message = "结束时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "end_date", nullable = false)
    private LocalDateTime endDate;

    @NotNull(message = "完成度评级不能为空")
    @Min(value = 1, message = "完成度评级最小为1")
    @Max(value = 5, message = "完成度评级最大为5")
    @Column(name = "completion_rating", nullable = false)
    private Integer completionRating;

    @CreationTimestamp
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;

    @UpdateTimestamp
    @Column(name = "update_time")
    private LocalDateTime updateTime;

    // 关联用户
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    private User user;

    // 校验结束时间不早于开始时间
    public boolean isDateRangeValid() {
        return startDate != null && endDate != null && !endDate.isBefore(startDate);
    }

    // Getter and Setter methods
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }

    public String getAffairName() { return affairName; }
    public void setAffairName(String affairName) { this.affairName = affairName; }

    public String getAffairCode() { return affairCode; }
    public void setAffairCode(String affairCode) { this.affairCode = affairCode; }

    public LocalDateTime getStartDate() { return startDate; }
    public void setStartDate(LocalDateTime startDate) { this.startDate = startDate; }

    public LocalDateTime getEndDate() { return endDate; }
    public void setEndDate(LocalDateTime endDate) { this.endDate = endDate; }

    public Integer getCompletionRating() { return completionRating; }
    public void setCompletionRating(Integer completionRating) { this.completionRating = completionRating; }

    public LocalDateTime getCreateTime() { return createTime; }
    public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }

    public LocalDateTime getUpdateTime() { return updateTime; }
    public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }

    public User getUser() { return user; }
    public void setUser(User user) { this.user = user; }
}